// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// l [author] rhyme_lph
// G [email]  rhymelph@gmail.com
// s [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/product_entity.dart';
import 'package:finance_app/src/entities/services_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/form/discount_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/number_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/switch/normal_switch.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/products_services/quantity_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

export 'package:finance_app/src/entities/product_entity.dart';
export 'package:finance_app/src/entities/services_entity.dart';

@RouterMeta(paramName: "quantityPage")
class QuantityPage extends StatefulWidget {
  final ServicesEntity service;
  final ProductEntity product;
  final int initialNumber;
  final bool usePurchasePrice;

  const QuantityPage({
    Key key,
    this.service,
    this.product,
    this.initialNumber,
    this.usePurchasePrice,
  }) : super(key: key);

  @override
  _QuantityPageState createState() => _QuantityPageState();
}

class _QuantityPageState extends State<QuantityPage>
    with PagePresenter<QuantityPage, QuantityProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.quantity, actions: [
        if (widget.initialNumber != null) buildDeleteBtn(),
      ]);

  Widget buildDeleteBtn() {
    return NormalDeleteTextButton(
      onTap: provider.onDelete,
    );
  }

  @override
  void initState() {
    super.initState();
    provider.initData(widget.product, widget.service, widget.initialNumber,
        widget.usePurchasePrice ?? false);
  }

  @override
  Widget get buildWidget => ListView(
        children: [
          FromInputField(
            controller: provider.nameController,
            focusNode: provider.nameFocusNode,
            tip: S.current.name__description,
          ),
          listBuilder<bool>(
            selector: (_, __) =>
                widget.initialNumber != null && __.gstInclusive == true,
            builder: (BuildContext context, bool value, Widget child) =>
                FromInputField(
              controller: provider.unitPriceController,
              focusNode: provider.unitPriceFocusNode,
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              inputFormatterList: [
                doubleInputFormatter,
              ],
              tip: S.current
                  .unit_price(value ? '(${S.current.includes_gst})' : ''),
            ),
          ),
          Visibility(
              visible: Config.currentCompany.enableGst == 1,
              child: Column(
                children: [
                  listBuilder<bool>(
                    selector: (_, __) => __.gstInclusive,
                    builder: (BuildContext context, bool value, Widget child) =>
                        ValueSelectForm(
                      title: S.current.gst_inclusive,
                      value: value == true ? S.current.yes : S.current.no,
                      onTap: provider.onChangeGstInclusive,
                    ),
                  ),
                  listBuilder<SettingDataTax>(
                    selector: (_, __) => __.gstRate,
                    builder: (BuildContext context, SettingDataTax value,
                            Widget child) =>
                        ValueSelectForm(
                      title: S.current.gst_rate,
                      value: '${value?.rate}% ${value?.key}',
                      onTap: provider.onChangeGstRate,
                    ),
                  ),
                ],
              )),
          NumberForm(
            key: ValueKey('Quantity'),
            title: S.current.quantity,
            initialNumber: widget.initialNumber,
            onNumberCallback: provider.onSelectNumber,
            canZero: false,
            hasBottomBorder: true,
          ),
          listBuilder(
            builder: (BuildContext context, bool value, Widget child) => Column(
              children: [
                NormalFormWrapper(
                  child: PressedOpacityButton(
                    onPressed: provider.onChangeDiscount,
                    child: NormalSwitch(
                      title: S.current.discount,
                      isSelect: value,
                    ),
                  ),
                ),
                if (value)
                  AddDiscountForm(
                    isCheckPercentage: provider.isCheckPercentage,
                    controller: provider.discountController,
                    focusNode: provider.discountFocusNode,
                    onChange: provider.onChangeIsPercentage,
                  ),
              ],
            ),
            selector: (_, __) => __.enabledDiscount,
          ),
          SizedBox(
            height: getDp(30),
          ),
          NormalButton(
            title: widget.initialNumber != null
                ? S.current.done
                : S.current.add_item,
            onTap: provider.onAddItem,
          )
        ],
      );
}
